package com.jinyang.jetpackdemo.activity;

import static android.os.Debug.startMethodTracing;
import static android.os.Debug.stopMethodTracing;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;
import android.os.Debug;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.jinyang.jetpackdemo.R;
import com.jinyang.jetpackdemo.util.LjyLogUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import kotlin.jvm.Throws;

public class LeakCanaryActivity extends AppCompatActivity {
    private Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            LjyLogUtil.d("msg.what:" + msg.what);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DateFormat dateFormat = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss", Locale.getDefault());
        String loadDate = dateFormat.format(new Date());
        Debug.startMethodTracing("LeakCanaryActivityTrace_" + loadDate);
        //示例剖析
//        Debug.startMethodTracingSampling("LeakCanaryActivityTrace",60,60);
        setContentView(R.layout.activity_leak_canary);
//        handler.sendEmptyMessageDelayed(123,60_000);
//        testHandlerThread();
//        doSomethingAndRelease();

        LiveEventBus
                .get("key1", Integer.class)
                .observe(this, value -> {
                    LjyLogUtil.d("value:"+value);
                });
        LiveEventBus
                .get("key1")
                .post(1001);
    }

    void doSomethingAndRelease() {
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "MyApp::MyWakelockTag");
        try {
            wakeLock.acquire();
            doSomethingThatThrows();
        } finally {
            wakeLock.release();
        }
    }

    private void doSomethingThatThrows() {
        for (int i = 0; i < 1000_000_000; i++) {
            String num=i+"";
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        try {
//            Thread.sleep(1000000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    private void testHandlerThread() {
        HandlerThread handlerThread = new HandlerThread("num++");
        handlerThread.start();
        Handler handler = new Handler(handlerThread.getLooper(), new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                LjyLogUtil.d(Thread.currentThread() + "子线程工作:" + msg.what);
                Message message = Message.obtain();
                message.what = msg.what * 10;
                mainHandler.sendMessage(message);
                return false;
            }
        });
        for (int i = 0; i < 5; i++) {
            handler.sendEmptyMessageDelayed(i, 1000);
        }
    }

    Handler mainHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            LjyLogUtil.d(Thread.currentThread() + "更新UI:" + msg.what);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Debug.stopMethodTracing();
    }
}